﻿using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Business.Bill.Utils;
using Kingdee.BOS.BusinessEntity.ConvertTracker;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.K3.Core.MFG.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace K.KDL
{
    [Description("展示单据")]
    public class CustomFormPluginForShowBill : AbstractDynamicFormPlugIn
    {
        //FormMetadata _arMeta;

        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);

            if (e.BarItemKey.EqualsIgnoreCase("bShowSYD"))
            {
                var billNo = this.View.Model.DataObject[""]?.ToString();
                ShowBill("", billNo);
            }

            if (e.BarItemKey.EqualsIgnoreCase("bShowBGD"))
            {
                var billNo = this.View.Model.DataObject[""]?.ToString();
                ShowBill("", billNo);
            }
        }

        private void ShowBill(string formId, string billNo)
        {
            var queryParam = new QueryBuilderParemeter();
            queryParam.SelectItems.Add(new SelectorItemInfo("FID"));
            queryParam.FilterClauseWihtKey = $" FBillNo = '{billNo}' ";
            queryParam.FormId = formId;
            if (!formId.IsNullOrEmptyOrWhiteSpace())
            {
                var objs = QueryServiceHelper.GetDynamicObjectCollection(this.Context, queryParam);
                if (objs != null && objs.Count > 0)
                {
                    long billId = Convert.ToInt64(objs[0][0]);
                    MFGBillUtil.ShowBillForm(this.View, formId, billId, OperationStatus.VIEW);
                }
                else
                {
                    this.View.ShowWarnningMessage($"系统中找不到编码为[{billNo}]的单据");
                }
            }
        }
    }
}
